package com.cn.aam.checaiduo.ui.me.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.base.adapter.BaseRecycerAdapter;
import com.cn.aam.checaiduo.base.adapter.holder.BaseRecyclerViewHolder;
import com.cn.aam.checaiduo.bean.me.PointDetailB;
import com.cn.aam.checaiduo.utils.CalendarUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

import static android.view.animation.Animation.RELATIVE_TO_SELF;
import static com.cn.aam.checaiduo.R.id.point_tv_month;

/**
 * monker on 2017/6/20.
 */

public class PointDetailAdapter extends BaseRecycerAdapter<PointDetailB> {

    LinearLayoutManager layoutManager;
    RecyclerView recyclerView;
    private int mScrollY = 0;

    private Map<String, List<PointDetailB>> map = new LinkedHashMap<>();
    private List<String> timeList = new ArrayList<>();
    private PointDetailChildAdapter adapter;

    private Map<String, PointDetailChildAdapter> childAdapters;

    public PointDetailAdapter(Context mContext) {
        super(mContext);
        childAdapters = new HashMap<>();
        adapter = new PointDetailChildAdapter(mContext);
    }


    @Override
    public void addData(List<PointDetailB> data) {
        if (data == null) {
            return;
        }

        getDataList().addAll(data);
        sortDate();
        notifyDataSetChanged();
    }

    @Override
    public void setDataToAdapter(List<PointDetailB> data) {
        getDataList().clear();
        childAdapters.clear();
        map.clear();
        timeList.clear();
        if (data != null && data.size() > 0) {
            getDataList().addAll(data);
            sortDate();
            map.get(timeList.get(0)).get(0).setExpand(true);//默认展开第一个item

        }
        notifyDataSetChanged();
        layoutManager.scrollToPositionWithOffset(0, -50);
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        this.recyclerView = recyclerView;
    }

    @Override
    public int getItemCount() {
        int newCount = super.getItemCount() - getDataList().size();
        return (newCount + timeList.size());
    }

    @Override
    protected BaseRecyclerViewHolder getViewHolder(View picNewsView) {
        return new PointDetailHolder(picNewsView);
    }

    @Override
    protected int getItemLayoutRes() {
        return R.layout.list_point_detail_item;
    }

    @Override
    protected void ItemVIewChange(BaseRecyclerViewHolder recyclerholder, final int position) {
        final PointDetailHolder holder = (PointDetailHolder) recyclerholder;
        final String key = timeList.get(position);
        holder.pointTvMonth.setText(key);

        List<PointDetailB> chidlList = map.get(key);

        if (chidlList == null) return;

        if (chidlList.get(0).isExpand()) {
            adapter = createAdapter(key);
            holder.pointRecycler.setAdapter(adapter);
            adapter.addData(chidlList);
            holder.pointRecycler.setVisibility(View.VISIBLE);
            animateExpand(holder.ivArrow);
        } else {
            holder.pointRecycler.setVisibility(View.GONE);
            animateCollapse(holder.ivArrow);
        }

        holder.ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isExpand = map.get(key).get(0).isExpand();
                map.get(key).get(0).setExpand(!isExpand);
                notifyItemChanged(position);
            }
        });
    }


    private PointDetailChildAdapter createAdapter(String key) {
        adapter = childAdapters.get(key);
        if (adapter == null) {
            adapter = new PointDetailChildAdapter(mContext);
            childAdapters.put(key, adapter);
        }
        return adapter;
    }


    class PointDetailHolder extends BaseRecyclerViewHolder {
        @BindView(point_tv_month)
        TextView pointTvMonth;
        @BindView(R.id.point_recycler)
        RecyclerView pointRecycler;
        @BindView(R.id.point_ll_header)
        LinearLayout ll;

        @BindView(R.id.point_iv_arrow)
        ImageView ivArrow;

        public PointDetailHolder(View itemView) {
            super(itemView);
            LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
            pointRecycler.setLayoutManager(layoutManager);
        }
    }

    /**
     * 时间：2017/6/21
     * 描述：分类
     */
    private void sortDate() {
        for (PointDetailB item : getDataList()) {
            String time = CalendarUtil.getYearAndMonth(item.getCreated_time());

            List<PointDetailB> list;
            if (map.get(time) == null) {
                timeList.add(time);
                list = new ArrayList<>();
            } else {
                list = map.get(time);
            }
            if (!list.contains(item)) {
                list.add(item);
                map.put(time, list);
            }
        }
    }

    private void animateExpand(View v) {
        RotateAnimation rotate =
                new RotateAnimation(360, 180, RELATIVE_TO_SELF, 0.5f, RELATIVE_TO_SELF, 0.5f);
        rotate.setDuration(300);
        rotate.setFillAfter(true);
        v.setAnimation(rotate);
    }

    private void animateCollapse(View v) {
        RotateAnimation rotate =
                new RotateAnimation(180, 360, RELATIVE_TO_SELF, 0.5f, RELATIVE_TO_SELF, 0.5f);
        rotate.setDuration(300);
        rotate.setFillAfter(true);
        v.setAnimation(rotate);
    }

    /**
     * 时间：2017/6/23
     * 描述：获取recycler滚动的距离
     */
    public int getScollYDistance() {

        int position = layoutManager.findFirstVisibleItemPosition();
        View firstVisiableChildView = layoutManager.findViewByPosition(position);
        int itemHeight = firstVisiableChildView.getHeight();
        return (position) * itemHeight - firstVisiableChildView.getTop();
    }

}
